<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    let str = '3[3[a]4[2[b]2[c]]]'

    function smartRepeat(templateStr) {
      let index = 0; // 指针
      let stack1 = []; // 栈1：存放数字
      let stack2 = []; // 栈2: 存放字符串
      // 遍历字符串
      while (index < templateStr.length - 1) {
        index++ // 指针后移
        console.log(templateStr[index]);
      }
    }

    smartRepeat(str)
  </script>
</body>

</html>